import threading
import time
import multiprocessing
from queue import Queue
num = 10000


def f1(q, a):
    res = 0
    # print(a)
    for i in range(a):
        res += i ^ 2+3*i+2

    q.put(res)
    print(res)


def mutil_thread():
    threads = []
    q = Queue()
    for i in range(4):
        t = threading.Thread(target=f1, args=(q, i*num+1,))
        t.start()
        threads.append(t)
    for t in threads:
        t.join()


def normal():
    q = Queue()
    for i in range(4):
        f1(q, i*num+1)


def mutil_processing():
    process_list = []
    q = multiprocessing.Queue()
    for i in range(4):
        p = multiprocessing.Process(target=f1, args=(q, i*num+1,))
        p.start()
        process_list.append(p)
    for p in process_list:
        p.join()


def mutil_processing_pool():
    q = multiprocessing.Queue()
    pool = multiprocessing.Pool(4)
    for i in range(4):
        pool.apply(f1, args=(q, i*num+1))
    pool.close()
    pool.join()


if __name__ == "__main__":
    # print(threading.activeCount())
    # print(threading.enumerate())
    t1 = time.time()
    q1 = mutil_thread()
    print(time.time()-t1)

    t2 = time.time()
    normal()
    print(time.time()-t2)

    t3 = time.time()
    q3 = mutil_processing()
    print('multil processing costs:', time.time()-t3)

    t4 = time.time()
    mutil_processing_pool()
    print('multil processing_pool costs:', time.time()-t4)
